package com.aaa.dao.impl;

import com.aaa.dao.UserInfoDao;
import com.aaa.util.BaseDao;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2021/7/14 16:29
 * @description：用户信息dao实现类
 * @modified By：
 * @version: 1.0
 */
public class UserInfoDaoImpl extends BaseDao implements UserInfoDao {
    @Override
    public boolean addUser(Map<String, Object> map) {
        String sql = "insert into userinfo (username ,password,createtime) values (?,?,?)";
        Object[] params = {map.get("username"), map.get("password"), map.get("createtime")};
        return super.executeUpdate(sql, params);
    }

    @Override
    public boolean deleteUserById(int id) {
        String sql = "delete from userinfo where id=?";
        Object[] params = {id};
        return super.executeUpdate(sql, params);
    }

    @Override
    public boolean deleteUserBatch(String ids) {
        String  sql="delete  from userinfo where id in("+ids+")";
        return  super.executeUpdate(sql, null);
    }

    @Override
    public boolean updateUser(Map<String, Object> map) {
        String sql = "update userinfo set username=?,password=?,createtime=? where id=?";
        Object[] params = {map.get("username"), map.get("password"), map.get("createtime"), map.get("id")};
        return super.executeUpdate(sql, params);
    }

    @Override
    public List<Map<String, Object>> findAllUser() {
        String sql = "select * from userinfo ";
        return super.executeQuery(sql, null);
    }


    @Override
    public Map<String, Object> findUserById(int id) {
        String sql = "select * from userinfo  where id=?";
        Object[] params = {id};
        List<Map<String, Object>> mapList = executeQuery(sql, params);
        if (mapList != null && mapList.size() > 0) {
            return mapList.get(0);
        }
        return null;
    }

    @Override
    public boolean findUserByUsername(String username) {
        String sql = "select * from userinfo  where username=?";
        Object[] params = {username};
        List<Map<String, Object>> mapList = executeQuery(sql, params);
        if (mapList != null && mapList.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public List<Map<String, Object>> findAllUserPage(Map map) {
      /* StringBuffer stringBuffer = new StringBuffer(" select * from   userinfo where 1=1 ");*/
        String  sql="select * from userinfo ";
        String makeSql = super.makeSql(sql, map);
        return super.executeQuery(makeSql, null);
    }

    @Override
    public int getCount(Map map ) {
        StringBuffer stringBuffer = new StringBuffer(" select  count(*) myCount from userinfo where 1=1 ");
        //拼接where条件  where username like  '%2%'
        Set set = map.keySet();
        for (Object o : set) {
            String  key=o.toString();
            if(key.contains("search")){
                //如果map的key中包含和关键字search，那么就是条件查询的条件
                stringBuffer.append(" and "+key.replace("search","")+" like "+"'%"+map.get(key)+"%'");
            }
        }
        List<Map<String, Object>> mapList = super.executeQuery(stringBuffer.toString(), null);
        if (mapList != null && mapList.size() > 0) {
            //获取第一行数据
            Map<String, Object> objectMap = mapList.get(0);
            //获取列名为myCount的列值
            Object count = objectMap.get("myCount");
            //将object类型的列值转换成int类型并返回
            return Integer.parseInt(count + "");
        }
        return 0;
    }
}
